package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.AdminMapper;
import com.hqyj.pojo.Admin;
import com.hqyj.service.AdminService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.HashMap;


@Service
public class AdminServiceImpl implements AdminService {

    //注入mapper
    @Autowired(required = false)
    AdminMapper adminMapper;


    @Override
    public HashMap<String, Object> select(Admin a) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //定义分页对象
        Page<Admin> page = new Page<>(a.getPage(),a.getRow());

        //条件构造器
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(a.getUserId()!=null,"user_id",a.getUserId());
        queryWrapper.like(a.getName()!=null&&!a.getName().equals(""),"name",a.getName());

            //查询数据库
        Page<Admin> aList =adminMapper.selectPage(page,queryWrapper);
            //当前页集合
        map.put("list",aList.getRecords());
            //总页数
        map.put("totalPage",aList.getPages());
            //总条数
        map.put("total",aList.getTotal());
            //当前页
        map.put("curPage",a.getPage());
        if(a.getPage()==1){
                //上一页
                map.put("prePage",1);

        }else{
                map.put("prePage",a.getPage()-1);

        }
        if(a.getPage()==aList.getPages()){
                //下一页
                map.put("nextPage",aList.getPages());

        }else{
                map.put("nextPage",a.getPage()+1);

        }
            //每页显示条数
          map.put("row",a.getRow());

        return map;
    }

    @Override
    public HashMap<String, Object> add(Admin a) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        /*//条件构造器
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(a.getUserName() != null, "username", a.getUserName());
        //查询数据库
        List<Admin> aList = adminMapper.selectList(queryWrapper);
        if (aList.size() > 0) {
            map.put("info", "用户名重名");
        } else {*/
            //新增
            int num = adminMapper.insert(a);
            if (num > 0) {
                map.put("info", "保存成功");
            } else {
                map.put("info", "保存错误");
            }
       // }
        return map;

        }
    @Override
    public HashMap<String, Object> update (Admin a){
        HashMap<String, Object> map = new HashMap<String, Object>();

        //修改
        int num = adminMapper.updateById(a);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del (Integer a){
        HashMap<String, Object> map = new HashMap<String, Object>();
        //删除
        int num = adminMapper.deleteById(a);
        if (num > 0) {
            map.put("info", "删除成功");
        } else {
            map.put("info", "保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> login(Admin a) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        /*//创建一个令牌
        UsernamePasswordToken token = new UsernamePasswordToken(a.getUserName(),a.getPassWord());
        Subject su = SecurityUtils.getSubject();
        try {
            //登录
            su.login(token);
            //存入登录用户信息
            HttpSession session = request.getSession();
            session.setAttribute("user",user);
            map.put("info",true);
        } catch(IncorrectCredentialsException ie){
            //密码异常
            System.err.println("密码不正确");
            map.put("info","密码不正确");
        }catch (UnknownAccountException e){
            e.printStackTrace();
            map.put("info","用户名输入错误");
        }*/


        return map;
    }
}
